import requests

# 适用于页面源代码里面没有数据而是在新的url或是在json格式里的数据
# 本节重点 防盗链 ==> Referer:https://www.pearvideo.com/video_1514319

# 播放视频的的url
tv = "https://video.pearvideo.com/mp4/short/20240205/cont-1791773-16018470-hd.mp4"
# 预览的视频url
tm = "https://video.pearvideo.com/mp4/short/20240205/1707292140403-16018470-hd.mp4"
print("--------------------------------------------------------------------------------------------")

url = f"https://www.pearvideo.com/video_1514319"
contid = url.split("_")[1]
# 获取页面json的url
videoStatus = f"https://www.pearvideo.com/videoStatus.jsp?contId={contid}&mrd=0.3949235626443586"
head = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0",
    # 处理防盗链 【Referer】 溯源，本次请求的上一级是谁
    "Referer": url
}

rs_pesp = requests.get(videoStatus, headers=head)
# 解析json()
js = rs_pesp.json()
systemTime = js["systemTime"]
tv_href = js["videoInfo"]["videos"]["srcUrl"]
tv_href = tv_href.replace(systemTime, "cont-" + contid)
print(tv_href)
# 下载视频
f = open(r"D:\User_Data\Open_Data\Pycharms\Datapy\爬虫知识点\4课程学习\get_data\庙会.mp4", "wb")
f.write(requests.get(tv_href).content)
f.close()
print("视频爬取完毕！！！")
